<template>
  <div id="detail">
    <detail-nav-bar
      class="top-nav"
      @titleClick="titleClick"
      ref="detailnav"
    ></detail-nav-bar>
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
    >
      <!-- <div>{{ this.$store.state.cartList.length }}</div> -->
      <!-- <ul>
        <li v-for="(item, index) in this.$store.state.cartList" :key="index">
          {{ item }}
        </li>
      </ul> -->
      <detail-swiper :topImages="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info
        :detailInfo="detailInfo"
        @imageLoad="imageLoad"
      ></detail-goods-info>
      <detail-param-info
        ref="params"
        :paramInfo="paramInfo"
      ></detail-param-info>
      <detail-comment-info
        ref="comment"
        :commentInfo="commentInfo"
      ></detail-comment-info>
      <goods-list ref="recommend" :goods="recommends"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <!-- <span class="count-show" v-show="isCountShow">
      <span class="count-span">{{ $store.state.message }}</span>
    </span> -->
    <!-- <toast></toast> -->
  </div>
</template>

<script>
import DetailNavBar from "./childComps/DetailNavBar.vue";
import DetailSwiper from "./childComps/DetailSwiper.vue";
import DetailBaseInfo from "./childComps/DetailBaseInfo.vue";
import DetailShopInfo from "./childComps/DetailShopInfo.vue";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo.vue";
import DetailParamInfo from "./childComps/DetailParamInfo.vue";
import DetailCommentInfo from "./childComps/DetailCommentInfo.vue";
import DetailBottomBar from "./childComps/DetailBottomBar.vue";

import Scroll from "components/common/scroll/Scroll.vue";
import GoodsList from "components/content/goods/GoodsList.vue";
import BackTop from "components/content/backTop/BackTop.vue";
// import Toast from "components/common/toast/Toast.vue";

import {
  getDetail,
  getRecommend,
  Goods,
  Shop,
  GoodsParam,
} from "network/detail.js";
// import Scroll from "../../components/common/scroll/Scroll.vue";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar,
    Scroll,
    GoodsList,
    BackTop,
    // Toast,
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      currentIndex: 0,
      isShowBackTop: false,
      isCountShow: false,
      themeTopYs: [],
    };
  },
  methods: {
    backClick() {
      this.$refs.scroll.scrollTo(0, 0);
    },
    contentScroll(position) {
      this.isShowBackTop = -position.y > 1000;
      // console.log(this.$refs.detailnav.currentIndex);
      for (let i = 0; i < this.themeTopYs.length; i++) {
        if (
          this.currentIndex !== i &&
          -position.y >= this.themeTopYs[i] &&
          -position.y < this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;
          this.$refs.detailnav.currentIndex = this.currentIndex;
        }
      }
    },
    loadMore() {
      console.log("pullingUp");
    },
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 300);
    },
    imageLoad() {
      this.themeTopYs = [];
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.params.$el.offsetTop);
      // this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      // console.log(this.$refs.comment.$el.offsetTop);
      // console.log(this.$refs.recommend.$el.offsetTop);
      // let commentOffsetTop = this.$refs.comment.$el
      //   ? this.$refs.comment.$el.offsetTop
      //   : this.$refs.recommend.$el.offsetTop;
      // console.log(commentOffsetTop);
      // console.log(typeof this.$refs.comment.$el);
      let commentOffsetTop = 0;
      if (typeof this.$refs.comment.$el.offsetTop !== "undefined") {
        commentOffsetTop = this.$refs.comment.$el.offsetTop;
        // console.log(commentOffsetTop);
      } else {
        commentOffsetTop = this.$refs.recommend.$el.offsetTop;
        // console.log(commentOffsetTop);
      }
      this.themeTopYs.push(commentOffsetTop);
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
      this.themeTopYs.push(Number.MAX_VALUE);
      // console.log(this.themeTopYs);
    },
    addToCart() {
      const userFlag = window.sessionStorage.getItem("username");
      if (!userFlag) {
        this.$toast.show("请登录后再加入购物车", 1000);
        setTimeout(() => {
          this.$router.push({
            path: "/login",
          });
        }, 1000);
      } else {
        // 1.获取购物车需要展示的信息
        const product = {};
        product.image = this.topImages[0];
        product.title = this.goods.title;
        product.desc = this.goods.desc;
        product.price = this.goods.realPrice;
        product.iid = this.iid;
        product.isChecked = false;
        product.isDelete = false;
        product.message = "";
        // console.log(product);

        // 2.修改store状态
        // this.$store.commit("addCart", product);
        this.$store.dispatch("addCart", product);
        this.isCountShow = true;
        setTimeout(() => {
          this.isCountShow = false;
        }, 2000);
        this.$toast.show(this.$store.state.message, 2000);
      }
    },
  },

  created() {
    // 1.保存传入的iid
    this.iid = this.$route.params.iid;

    // 2.根据iid请求详情数据
    getDetail(this.iid).then((res) => {
      // 1.获取顶部的图片轮播数据
      // console.log(res);
      const data = res.result;
      this.topImages = data.itemInfo.topImages;

      // 2.获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );

      // 3.创建店铺信用
      this.shop = new Shop(data.shopInfo);

      // 4.保存商品的详细数据
      this.detailInfo = data.detailInfo;

      // 5.获取参数的信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );
      // 6.获取评论信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }
      //  图片未加载完
      // this.$nextTick(() => {
      //   this.themeTopYs = [];
      //   this.themeTopYs.push(0);
      //   this.themeTopYs.push(this.$refs.params.$el.offsetTop);
      //   this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      //   this.themeTopYs.push(this.$refs.remmen.$el.offsetTop);
      //   console.log(this.themeTopYs);
      // });
    });

    // 3.请求推荐数据
    getRecommend().then((res) => {
      // console.log(res);
      this.recommends = res.data.list;
    });
  },
  mounted() {
    // console.log(this.$refs.scroll);
  },

  // activated() {
  //   getDetail(this.iid).then((res) => {
  //     console.log(res);
  //     this.topImages = res.result.itemInfo.topImages;
  //   });
  // },
};
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 11;
  background-color: #fff;
  height: 100vh;
}
.top-nav {
  background-color: #fff;
  position: relative;
  top: 0;
  left: 0;
  right: 0;
  z-index: 11;
}
.content {
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}
/* .content {
  height: calc(100% - 44px);
} */
.count-show {
  position: absolute;
  top: 50%;
  left: 50%;
}
.count-span {
  position: relative;
  top: -50%;
  left: -50%;
  padding: 5px 10px;
  background-color: rgba(3, 3, 3, 0.5);
  border-radius: 5px;
  font-size: 14px;
  color: #fff;
}
</style>